//
//  KWTPlantMasterDeviceListVC.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/8/9.
//

#import "KWTPlantMasterDeviceListVC.h"
#import "KWTMGRNSettingSyncCell.h"
#import "RHDeviceModel.h"
#import "KWTMGRNSettingVC.h"
#import "RHEndPointModel.h"
#import "KWT233KInvSettingVC.h"
#import "RHSolarkSettingVC.h"
#import "RHHiSolarSettingVC.h"
#import "RHTools.h"

@interface KWTPlantMasterDeviceListVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@end

@implementation KWTPlantMasterDeviceListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"设备列表".rh_localized;
    [self setupUI];
}

- (void)setupUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setBackgroundColor:CFTabViewBgColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[KWTMGRNSettingSyncCell class] forCellReuseIdentifier:@"KWTMGRNSettingSyncCell"];
    _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, 8)];
    [self.view addSubview:_tableView];
    _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"nodata_icon_1" titleStr:@"没有数据".rh_localized detailStr:@""];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
   
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.deviceArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KWTMGRNSettingSyncCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KWTMGRNSettingSyncCell"];

    RHDeviceModel *device = self.deviceArr[indexPath.row];
    cell.deviceModel = device;
    cell.selectIcon.hidden = YES;
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RHDeviceModel *device = self.deviceArr[indexPath.row];
    
    if(device.lastOnlineTime == 0){
        [QMUITips showError:@"请让设备先连接云平台".rh_localized];
        return;
    }
    [self getEndPointReration:device];
}


#pragma mark -----1. 获取设备端点关系 -------
- (void)getEndPointReration:(RHDeviceModel*)device{
    [QMUITips showLoadingInView:self.view];
    NSString *url = [NSString stringWithFormat:@"%@/%@",H_getDevRelationTypeList,SafeStr(device.deviceId)];
    [RHEndPointTypeModel startGetWithCompletionAPIKey:url argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if(resultModel.code == 200){
        
            NSArray *typeArr = resultModel.data;
            
            __block BOOL canConfig = NO;
            [typeArr enumerateObjectsUsingBlock:^(RHEndPointTypeModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSArray *allInvType = [RHTools allConfigedInverterType];
                if ([allInvType containsObject:obj.endpoint]&&obj.nodes.count>0) {
                    RHEndPointModel *endpointModel = [obj.nodes firstObject];
                    if ([endpointModel.endpoint isEqualToString:InvType_InvMegarevoLv12KW]) {
                        canConfig = YES;
                        *stop = YES;
                        KWTMGRNSettingVC *settingVC = [[KWTMGRNSettingVC alloc]initWithDeviceModel:device endpoint:endpointModel];
                        settingVC.deviceArr = self.deviceArr;
                        [self.navigationController pushViewController:settingVC animated:YES];
                        
                    }else if([endpointModel.endpoint isEqualToString:InvType_PCS_BCS_B_HM_USX2]){
                        canConfig = YES;
                        *stop = YES;
                        KWT233KInvSettingVC *settingVC = [[KWT233KInvSettingVC alloc]initWithDeviceModel:device endpoint:endpointModel];
                        settingVC.deviceArr = self.deviceArr;
                        [self.navigationController pushViewController:settingVC animated:YES];
                        
                    }else if ([endpointModel.endpoint isEqualToString:InvType_Sol_Ark_30K_3P_208V]||
                        [endpointModel.endpoint isEqualToString:InvType_Sol_Ark_60K_3P_480V]) {//61度电 Solark
                        canConfig = YES;
                        *stop = YES;
                        RHSolarkSettingVC *solisVC = [[RHSolarkSettingVC alloc]initWithDeviceModel:device endpoint:endpointModel];
                        solisVC.deviceArr = self.deviceArr;
                        [self.navigationController pushViewController:solisVC animated:YES];
                        
                    }else if([endpointModel.endpoint isEqualToString:InvType_InvHISOLAR15KW]){//海索
                        canConfig = YES;
                        *stop = YES;
                        RHHiSolarSettingVC *hiSolarVC = [[RHHiSolarSettingVC alloc]initWithDeviceModel:device endpoint:endpointModel];
                        hiSolarVC.deviceArr = self.deviceArr;
                        [self.navigationController pushViewController:hiSolarVC animated:YES];
                        
                    }
                }
            }];
            
            if (canConfig == NO) {
                [QMUITips showInfo:@"设备不支持此功能".rh_localized];
            }
        }
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
    }];
    
    
}

#pragma mark ------- 2.获取逆变器类型 为日月元而设---------
-(void)getIverterType:(RHEndPointTypeModel*)endPointTypeModel device:(RHDeviceModel *)device
{
    if (endPointTypeModel.nodes.count==0) {
        return;
    }
    RHEndPointModel *endpointModel = [endPointTypeModel.nodes firstObject];
    [QMUITips showLoadingInView:self.view];
    [BaseModel startGetWithCompletionAPIKey:H_getParamStatusData argument:@{@"deviceId":ValidNullData(device.deviceId),@"sn":ValidNullData(endpointModel.sn)} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.tableView endHeaderRefresh];
        if(resultModel.code == 200){
            device.inverterType = (DevInverterType)[request.responseJSONObject[@"data"][@"Inv_DevType"] integerValue];
             if(device.inverterType == DevInverterTypeMGRN7K||
                device.inverterType == DevInverterTypeMGRN12K){
                 KWTMGRNSettingVC *settingVC = [[KWTMGRNSettingVC alloc]initWithDeviceModel:device endpoint:endPointTypeModel.nodes.firstObject];
        
                 settingVC.deviceArr = self.deviceArr;
                 [self.navigationController pushViewController:settingVC animated:YES];
            }else{
                [QMUITips showInfo:@"设备不支持此功能".rh_localized];
            }

        }
        
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
       
        
    }];
}
@end
